package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 兴业B2B网关支付报文拼装
 * 
 * @CibB2bManager.java
 * @author zhengxue
 * @2014-11-21 上午11:29:23 www.gopay.com.cn Inc.All rights reserved.
 */
public class CibB2bManager implements BankManagerRemoteService {
    @Resource(name = "cibB2bBankServiceClient")
    private BankService cibB2bBankServiceClient;

    @Resource(name = "cibConfig")
    private Map<String, String> cibConfig;

    /**
     * B2B签名
     */
    public Object sign(BaseMessage msg) throws GopayException {
        CibMessage messageB2B = (CibMessage) msg;
        messageB2B = buildSignMessage(messageB2B);
        BankLogUtils.printB2BManagerSignSendLog(messageB2B);

        String signature = (String) cibB2bBankServiceClient.sign(messageB2B);
        messageB2B.setMac(signature);
        BankLogUtils.printB2BManagerSignReturnLog(messageB2B);
        return messageB2B;
    }

    /**
     * B2B支付结果验签
     */
    public Object verify(BaseMessage msg) throws GopayException {
        CibMessage messageB2B = (CibMessage) msg;
        messageB2B = buildVerifyMessag(messageB2B);
        BankLogUtils.printB2BManagerVerifySendLog(messageB2B);

        boolean isVerify = (Boolean) cibB2bBankServiceClient.verify(messageB2B);
        BankLogUtils.printB2BManagerVerifyReturnLog(messageB2B, isVerify);
        return isVerify;
    }

    /**
     * B2B订单查询
     */
    public Object query(Object obj) throws GopayException {
        CibMessage messageB2B = (CibMessage) obj;
        messageB2B = buildQueryMessag(messageB2B);
        BankLogUtils.printB2BManagerQuerySendLog(messageB2B, "");

        messageB2B = (CibMessage) cibB2bBankServiceClient.query(messageB2B);
        BankLogUtils.printB2BManagerQueryReturnLog(messageB2B);
        return messageB2B;
    }

    /**
     * 组装B2B签名对象
     * 
     * @param messageB2B
     * @return
     */
    private CibMessage buildSignMessage(CibMessage messageB2B) {
        messageB2B.setBankUrl(messageB2B.getPayUrl());
        messageB2B.setService(cibConfig.get("CIB.B2B.SERVICE"));
        messageB2B.setVer(cibConfig.get("CIB.B2B.VER"));
        messageB2B.setOrdDate(messageB2B.getOrderDate().substring(0, 8));
        messageB2B.setCur(cibConfig.get("CIB.B2B.CUR"));
        messageB2B.setOrdAmt(messageB2B.getAmount().toString());
        messageB2B.setSubMrch("GopayB2B");
        if (StringUtils.isNotBlank(messageB2B.getPaymentAccInfo())) {
            messageB2B.setSubMrch(messageB2B.getPaymentAccInfo());
        }
        // 拼装签名明文
        String plainMessage = "service" + messageB2B.getService() + "ver" + messageB2B.getVer() + "mrch_no"
                + messageB2B.getMerID() + "sub_mrch" + messageB2B.getSubMrch() + "ord_no" + messageB2B.getOrderId() + "ord_date" + messageB2B.getOrdDate()
                + "cur" + messageB2B.getCur() + "ord_amt" + messageB2B.getOrdAmt() + messageB2B.getPrivateKeyPath();
        messageB2B.setPlainMessage(plainMessage.trim());
        return messageB2B;
    }

    /**
     * 组装B2B验签对象
     * 
     * @param verifyMessage
     * @return
     */
    private CibMessage buildVerifyMessag(CibMessage verifyMessage) {
        String plainMessage = "bank" + verifyMessage.getBankCode() + "mrch_no" + verifyMessage.getMrchNo() + "ord_no"
                + verifyMessage.getOrdNo() + "cur" + verifyMessage.getCur() + "ord_amt" + verifyMessage.getOrdAmt()
                + "pay_amt" + verifyMessage.getPayAmt() + "sno" + verifyMessage.getSno()
                + verifyMessage.getPrivateKeyPath();
        verifyMessage.setPlainMessage(plainMessage);
        return verifyMessage;
    }

    /**
     * 组装B2B订单查询对象
     * 
     * @param queryMessage
     * @return
     */
    private CibMessage buildQueryMessag(CibMessage queryMessage) {
        queryMessage.setQueryUrl(queryMessage.getQueryUrl());
        queryMessage.setService(cibConfig.get("CIB.QUERY.B2B.SERVICE"));
        queryMessage.setVer(cibConfig.get("CIB.B2B.VER"));
        // 拼装订单查询明文
        String plainMessage = "service" + queryMessage.getService() + "ver" + queryMessage.getVer() + "mrch_no"
                + queryMessage.getMerID() + "ord_no" + queryMessage.getOrderId() + queryMessage.getPrivateKeyPath();
        queryMessage.setPlainMessage(plainMessage);
        return queryMessage;
    }

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
